package top.so.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.so.dao.ColumnDao;
import top.so.dto.ColumnDTO;
import top.so.entity.Column;
import top.so.service.ColumnService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ColumnServiceImpl implements ColumnService {

    @Autowired
    private ColumnDao columnDao;

    public List<ColumnDTO> selectWithPage(ColumnDTO columnDTO,int pageNum) {
        List<Column> columnList = columnDao.selectWithPage(DTOToPOJO(columnDTO),pageNum);
        List<ColumnDTO> columnDTOList = POJOToDTO(columnList);
        return columnDTOList;
    }

    public List<ColumnDTO> selectOrderByReadNum(ColumnDTO columnDTO) {
        List<Column> columnList = columnDao.selectOrderByReadNum(DTOToPOJO(columnDTO));
        List<ColumnDTO> columnDTOList = POJOToDTO(columnList);
        return columnDTOList;
    }

    public List<ColumnDTO> select(ColumnDTO columnDTO) {
        List<Column> columnList = columnDao.select(DTOToPOJO(columnDTO));
        List<ColumnDTO> columnDTOList = POJOToDTO(columnList);
        return columnDTOList;
    }

    private List<ColumnDTO> POJOToDTO(List<Column> columnList) {
        List<ColumnDTO> columnDTOList = new ArrayList<ColumnDTO>();
        ColumnDTO temp;

            for(Column column:columnList){
                temp = new ColumnDTO();
                temp.setUserID(column.getUserID());
                temp.setColumnID(column.getColumnID());
                temp.setColumnName(column.getColumnName());
                temp.setColumnType(column.getColumnType());
                temp.setGenTime(column.getGenTime());
                temp.setReadNum(column.getReadNum());
                columnDTOList.add(temp);
            }
        return columnDTOList;
    }

    public boolean insertColumn(ColumnDTO columnDTO) {
        UUID uuid = UUID.randomUUID();
        columnDTO.setColumnID(String.valueOf(uuid).replaceAll("-",""));
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        columnDTO.setGenTime(formatter.format(date));
        return columnDao.insertColumn(DTOToPOJO(columnDTO));
    }

    private Column DTOToPOJO(ColumnDTO columnDTO) {
        Column column = new Column();
        if(columnDTO!=null){
            column.setColumnID(columnDTO.getColumnID());
            column.setColumnName(columnDTO.getColumnName());
            column.setColumnType(columnDTO.getColumnType());
            column.setGenTime(columnDTO.getGenTime());
            column.setReadNum(columnDTO.getReadNum());
            column.setUserID(columnDTO.getUserID());
        }
        return  column;
    }

    public boolean updateColumn(ColumnDTO columnDTO) {
        return columnDao.updateColumn(DTOToPOJO(columnDTO));
    }

    public boolean deleteColumn(ColumnDTO columnDTO) {
        return columnDao.deleteColumn(DTOToPOJO(columnDTO));
    }
}
